using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using usm.SolDev.DataBase.DataObjects;

namespace usm.SolDev.UI.User
{
    /// <summary>
    ///     Create new user and edit existing user data
    /// </summary>
    public partial class CreateUser : usm.SolDev.UI.BaseForm
    {
        #region Private fields
        private UserInfoData userInfoData;
        #endregion

        #region Constructors
        /// <summary>
        ///     Default constructor
        /// </summary>
        public CreateUser()
        {
            InitializeComponent();
        }

        /// <summary>
        ///     Initialization constructor.. The user data information.
        /// </summary>
        /// <param name="userInfoData"></param>
        public CreateUser(UserInfoData userInfoData, List<GroupDataObject> groups)
        {
            InitializeComponent();

            #region Set data
            this.userInfoData = userInfoData;

            if (userInfoData.Name != null)
            {
                tbFullName.Text = userInfoData.Name;
            }

            if (userInfoData.Login != null)
            {
                tbLogin.Text = userInfoData.Login;
            }

            foreach ( GroupDataObject group in groups )
            {
                cbGroup.Items.Add(group);
            }

            if (cbGroup.Items.Count > 0)
            {
                cbGroup.SelectedItem = cbGroup.Items[0];
            }

            tbPassword.Text = String.Empty;
            #endregion

            #region Decode object state
            switch (userInfoData.State)
            {
                case usm.SolDev.Common.Config.ObjectState.New:
                    Text = "New user";
                    if (cbGroup.Items.Count > 0)
                    {
                        cbGroup.Text = cbGroup.Items[0].ToString();
                    }
                    break;

                case usm.SolDev.Common.Config.ObjectState.Modified:
                    Text = "Edit user name";
                    break;

                case usm.SolDev.Common.Config.ObjectState.Original:
                    Text = "User data";
                    tbFullName.Enabled = false;
                    tbLogin.Enabled = false;
                    tbPassword.Enabled = false;
                    break;
            }
            #endregion
        }
        #endregion

        #region Form events handler
        /// <summary>
        ///     On OK button press, validate input data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            #region Validate data
            errorProvider.Clear();
            if (tbFullName.Text == String.Empty)
            {
                errorProvider.SetError(tbFullName, "Please specify user name");
                tbFullName.Focus();
                return;
            }

            if (cbGroup.SelectedItem == null)
            {
                errorProvider.SetError(cbGroup, "Please set a group to which user belongs to");
                cbGroup.Focus();
                return;
            }
            
            if (tbLogin.Text == String.Empty)
            {
                errorProvider.SetError(tbLogin, "Please set a login name");
                tbLogin.Focus();
                return;
            }

            if (tbPassword.Text == String.Empty)
            {
                errorProvider.SetError(tbPassword, "Please set a password");
                tbPassword.Focus();
                return;
            }
            if  (tbPassword.Text.Equals(tbPasswordConfirm.Text) == false)
            {
                errorProvider.SetError(tbPasswordConfirm, "The password does not match previous");
                tbPassword.Focus();
                return;
            }
            #endregion

            userInfoData.Name = tbFullName.Text;
            userInfoData.Group = cbGroup.SelectedIndex + 1;
            userInfoData.Login = tbLogin.Text;
            userInfoData.Password = tbPassword.Text;

            DialogResult = DialogResult.OK;
        }
        #endregion

        #region Public properties
        /// <summary>
        ///     The user data which has been modified 
        /// </summary>
        public UserInfoData UserInfoData
        {
            get { return userInfoData; }
            set { userInfoData = value; }
        }
        #endregion

    }
}

